package cn.lexi.vip.controller;

import cn.lexi.vip.exception.ControllerAccessVerifyException;
import cn.lexi.vip.exception.NullIpException;
import cn.lexi.vip.exception.NullUserException;
import cn.lexi.vip.exception.ResponseException;
import cn.lexi.vip.model.JsonResult;
import cn.lexi.vip.exception.ApplicationException;
import cn.lexi.vip.exception.ExceptionLogger;
import cn.lexi.vip.exception.IdCardException;
import cn.lexi.vip.exception.ResubmissionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Aaron
 * 出现异常，进入这个handler。
 */
@ControllerAdvice
@RestController
public class ExceptionHandlingController {

    private static final JsonResult CHANNEL_ID_EXCEPTION = new JsonResult("channelId获取失败", 1L, null);
    private static final JsonResult EXCEPTION_RESULT = new JsonResult("internal error", 1L, null);
    private static final JsonResult NULL_USER_EXCEPTION = new JsonResult("session已过时", 401L, null);
    private static final JsonResult XYQB_INCORRECT_STATE_EXCEPTION = new JsonResult("非法的状态", 1L, null, 1L);
    private static final JsonResult NULL_IP_EXCEPTION = new JsonResult("IP不能为空", 0L, null, 1L);
    private static final JsonResult RESUBMISSION_EXCEPTION = new JsonResult("操作过于频繁，请稍后再试", 0L, null, 1L);

    @Autowired
    private ExceptionLogger exceptionLogger;

    @ExceptionHandler(ResubmissionException.class)
    @ResponseStatus(HttpStatus.OK)
    public JsonResult resubmissionException(ResubmissionException re) {
        return RESUBMISSION_EXCEPTION;
    }

    @ExceptionHandler(NullIpException.class)
    @ResponseStatus(HttpStatus.OK)
    public JsonResult nullIpException(NullIpException nie) {
        exceptionLogger.warnException(nie);
        return NULL_IP_EXCEPTION;
    }

    @ExceptionHandler(IdCardException.class)
    @ResponseStatus(HttpStatus.OK)
    public JsonResult idCardException(IdCardException ice) {
        return new JsonResult(ice.getMessage(), 1L, null);
    }

    @ExceptionHandler(NullUserException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public JsonResult nullUserException(NullUserException nue) {

        return NULL_USER_EXCEPTION;
    }

    @ExceptionHandler(ApplicationException.class)
    @ResponseStatus(HttpStatus.OK)
    public JsonResult applicationException(ApplicationException ae) {
        exceptionLogger.warnException(ae);
        return EXCEPTION_RESULT;
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public JsonResult exceptionOccurs(Exception e) {
        exceptionLogger.logException(e);
        return EXCEPTION_RESULT;
    }

    @ExceptionHandler(ResponseException.class)
    @ResponseStatus(HttpStatus.OK)
    public JsonResult responseException(ResponseException e) {
        exceptionLogger.logException(e);
        return JsonResult.buildErrorStateResult(e.getMessage(), null);
    }

    @ExceptionHandler(ControllerAccessVerifyException.class)
    @ResponseStatus(HttpStatus.OK)
    public JsonResult controllerAccessVerifyException(ControllerAccessVerifyException e) {
        exceptionLogger.logException(e);
        return JsonResult.buildErrorStateResult("接口访问校验失败", null, 99L);
    }

}
